﻿using NHibernate;
using OneStationFile.IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace OneStationFile.WEB.Controllers
{
    public class ModularController : Controller
    {
        protected IBaseService BaseService { get; set; }
        // GET: Modular

        /// <summary>
        /// 模块中不存在的节点，用于添加。
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="kwName"></param>
        /// <returns></returns>
        public ActionResult FreeNodes(int id = 0, int page = 1, string kwName = "")
        {
            Model.Modular modular = BaseService.GetModelById<Model.Modular>(id);
            List<Model.Node> nodes = new List<Model.Node>();
            if (modular.Nodes!= null)
            {
                nodes = modular.Nodes.ToList();
            }
            List<Model.Node> allNodes = BaseService.GetModels<Model.Node>().List().Where(p=>p.NodeLevelD == Common.ListOfEnum.NodeLevelD.一级).ToList();
            if (nodes.Count()>=1)
            {
                allNodes = allNodes.Except(nodes).ToList();
            }

            PagedList<Model.Node> plm = allNodes.ToPagedList(page, 8);
            /// PagedList<Model.Modular> plm = bmu.GetListByPage(Where, "id desc ", page, 10).ToPagedList(page, 10);
            plm.CurrentPageIndex = page;
            plm.TotalItemCount = allNodes.Count();//总记录数
            return View(plm);
        }
        /// <summary>
        /// AddNodes 添加节点
        /// </summary>
        /// <param name="strId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddNodes(string strId,int id) {
            var result = new { msg = "添加成功！", res = true };
            if (string.IsNullOrEmpty(strId) || id<=0)
            {
                result = new { msg = "请确认选中对的不为空,并再次提交！", res = false };
                return Json(result);
            }
            var arr = strId.Split(',');
            
            Model.Modular modular = BaseService.GetModelById<Model.Modular>(id);
            foreach (string s in arr)
            {
                if (!string.IsNullOrEmpty(s))
                {
                    modular.Nodes.Add(BaseService.GetModelById<Model.Node>(Convert.ToInt32(s)));
                }
            }
            BaseService.SaveOrUpdate(modular);
            return Json(result);
        }
        /// <summary>
        /// 模块已选中的节点
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="kwName"></param>
        /// <returns></returns>
        public ActionResult Nodes(int id = 0, int page = 1, string kwName = "") {
            Model.Modular modular = BaseService.GetModelById<Model.Modular>(id);
            List<Model.Node> nodes = new List<Model.Node>();
            if (modular.Nodes!= null)
            {
                nodes = modular.Nodes.ToList();
            }
            PagedList<Model.Node> plm = nodes.ToPagedList(page, 8);
            /// PagedList<Model.Modular> plm = bmu.GetListByPage(Where, "id desc ", page, 10).ToPagedList(page, 10);
            plm.CurrentPageIndex = page;
            plm.TotalItemCount = nodes.Count();//总记录数
            return View(plm);
        }
        [HttpPost]
        /// <summary>
        /// DeleteNode 删除节点
        /// </summary>
        /// <param name="id"></param>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public JsonResult DeleteNode(int id,int nodeId) {
            var result = new { msg = "删除成功！", res = true };
            Model.Modular modular = BaseService.GetModelById<Model.Modular>(id);
            Model.Node node = BaseService.GetModelById<Model.Node>(nodeId);
            modular.Nodes.Remove(node);
            BaseService.SaveOrUpdate(modular);
            return Json(result);
        }
        /// <summary>
        /// Delete 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Delete(int id)
        {
            var result = new { msg = "删除成功!", res = true };
            if (id>0)
            {
                if (!BaseService.DeleteModelById<Model.Modular>(id))
                {
                    result = new { msg = "删除失败!", res = false };
                    return Json(result);
                }

            }
            return Json(result);
        }
        public ActionResult Index(int id = 0, int page = 1,string kwName = "")
        {

            List<Model.Modular> queryOver = BaseService.GetModels<Model.Modular>().List().ToList();
            if (!string.IsNullOrEmpty(kwName))
            {
                queryOver = queryOver.Where(p => p.Name.Contains(kwName)).ToList();
            }
            PagedList<Model.Modular> plm = queryOver.ToPagedList(page, 8);
            /// PagedList<Model.Modular> plm = bmu.GetListByPage(Where, "id desc ", page, 10).ToPagedList(page, 10);
            plm.CurrentPageIndex = page;
            plm.TotalItemCount = queryOver.Count();//总记录数
            return View(plm);
        }
        public ActionResult Edit(int id =0) {
            if (id>0)
            {
                ViewBag.action = "修改";
                Model.Modular modular = BaseService.GetModelById<Model.Modular>(id);
                return View(modular);
            }
            ViewBag.action = "添加";
            return View();
        }
        [HttpPost]
        public JsonResult EditHandle(Model.Modular model, FormCollection collection) {
            var result = new { msg = "操作成功!", res = true };
            Model.Modular modular = null;
            if (string.IsNullOrEmpty(model.Name))
            {
                result = new { msg = "名称不能为空", res = false };
                return Json(result);
            }
            if (string.IsNullOrEmpty(model.Describe))
            {
                result = new { msg = "描述不能为空", res = false };
                return Json(result);
            }
           
            if (model.Id>0)
            {
                if (BaseService.ExistModelByWhere<Model.Modular>(p => p.Name == model.Name)>=2)
                {
                    result = new { msg = "名称已经存在！", res = false };
                    return Json(result);
                }
                modular = BaseService.GetModelById<Model.Modular>(model.Id);
                try
                {
                    UpdateModel(modular);
                }
                catch (Exception ex)
                {

                    throw(ex);
                }
            }
            else
            {
                if (BaseService.ExistModelByWhere<Model.Modular>(p => p.Name == model.Name) >=1)
                {
                    result = new { msg = "名称已经存在！", res = false };
                    return Json(result);
                }
                modular = model;
                //modular.Nodes = null;
            }
            if (BaseService.SaveOrUpdate(modular) == false)
            {
                result = new { msg = "保存时，出现在错误", res = false };
                return Json(result);
            }
            return Json(result);
        }
    }
}